package cn.fl.system.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysDictionaryService;
import cn.fl.system.vo.SysDictionaryVO;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import fl.ref.fribaseinfo.constant.FriBaseInfoConstant;
import fl.ref.fribaseinfo.dto.FriBaseInfoDTO;
import fl.ref.fribaseinfo.service.IFriBaseInfoApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cmm.web.core.framework.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统数据字典表 控制层
 */
@Controller
@RequestMapping("/system/sysdictionary")
public class SysDictionaryController extends BaseController {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(SysDictionaryController.class);

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private IFriBaseInfoApiService friBaseInfoApiService;

	@RequestMapping(value = "/getSysDic.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getSysDic(@RequestParam(value = "orgCd",required = false) String orgCd,
							@RequestParam(value = "sysCd",required = false) String sysCd,
							@RequestParam(value = "codeGroup") String codeGroup,
							@RequestParam(value = "useDesc",defaultValue = "false") boolean useDesc) throws BaseException {

		if(StringUtils.isBlank(orgCd)){
			orgCd = CurrentThreadContext.getCurrentOrgCd();
		}
		if(StringUtils.isBlank(sysCd)){
			sysCd = CurrentThreadContext.getFlServiceSysCd();
		}

		List<KeyValueVO> list = null;
		if(useDesc) {
			List<SysDictionaryDTO> sysDics = sysDictionaryApiService.searchGroup(sysCd, orgCd,codeGroup);
			if(CollectionUtils.isNotEmpty(sysDics)) {
				list = new ArrayList<>();
				for(SysDictionaryDTO dto : sysDics){
					KeyValueVO kv = new KeyValueVO();
					kv.setKey(dto.getCode());
					kv.setValue(dto.getCodeDesc());
					list.add(kv);
				}
			}
		}else {
			list =  sysDictionaryApiService.getKvList(codeGroup,sysCd,orgCd);
		}
		if(null == list) {
			list = new ArrayList<>();
		}
		return list;
	}

	@RequestMapping(value = "/getInsCode.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getInsCode(@RequestParam(value = "orgCd",required = false) String orgCd,
							@RequestParam(value = "sysCd",required = false) String sysCd) throws BaseException {

		if(StringUtils.isBlank(orgCd)){
			orgCd = CurrentThreadContext.getCurrentOrgCd();
		}
		if(StringUtils.isBlank(sysCd)){
			sysCd = CurrentThreadContext.getFlServiceSysCd();
		}

		List<KeyValueVO> list =  new ArrayList<>();
		FriBaseInfoQueryCondition condition = new FriBaseInfoQueryCondition();
		condition.setInsUseCd("2");//只查询资管类型的融资机构
		condition.setInsSts(FriBaseInfoConstant.FRI_BASE_INFO_INS_STS_PASS);
		condition.setSysCd(sysCd);
		condition.setOrgCd(orgCd);
		List<FriBaseInfoDTO> friBaseInfoDTOList = friBaseInfoApiService.searchList(condition);
		System.out.println(JSON.toJSON(friBaseInfoDTOList));
		if(CollectionUtils.isNotEmpty(friBaseInfoDTOList)){
			for(FriBaseInfoDTO dto : friBaseInfoDTOList){
				KeyValueVO kv = new KeyValueVO();
				kv.setKey(dto.getInsCode());
				kv.setValue(dto.getFundRasInsNm());
				list.add(kv);
			}
		}
		return list;
	}
}

